package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @PackageName:
 * @ClassName: MenuController
 * @Author: sushengchao
 * @Date: 2019/12/11 12:38
 * @Description:
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private UserService userService;
    @Reference
    private MenuService menuService;

    @RequestMapping("/getMenus")
    public List<Menu> findMenus(){
        //得到当前登录用户的姓名,查询得到角色名集合
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //根据用户名得到角色集合
        Set<Role> set = userService.getRolesByUserName(user.getUsername());
        //根据角色查询得到角色对应的菜单
        List<Menu> menuList = menuService.findMenu(set);

        return menuList;
    }
}
